#include "tcpServer.hpp"
#include <memory>
using namespace tcpserver_ns;

Response calculate(const Request& req){
    //进入这个函数之前一定保证req中是有具体数据的（req已经被反序列化读取出了原始数据）
    Response res(0, 0);
    switch(req._op){
        case '+':
            res._result = req._x + req._y;
            break;
        case '-':
            res._result = req._x - req._y;
            break;
        case '*':
            res._result = req._x * req._y;
            break;
        case '/':
            if(req._y == 0){
                res._code = 1;
            }
            res._result = req._x / req._y;
            break;
        case '%':
            if(req._y == 0){
                res._code = 2;
            }
            res._result = req._x % req._y;
            break;
        default:
            logMessage(ERROR, "enter error!");
            res._code = 3;
            break;
    }

    return res;
}

int main(){
    uint16_t port = 8080;
    unique_ptr<TcpServer> tsvr(new TcpServer(calculate, port));
    tsvr->InitServer();
    tsvr->Start();

    return 0;
}